package com.holdtime.jtt809.taxi.module.web.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.holdtime.jtt809.taxi.module.entity.LocationNewDO;
import com.holdtime.jtt809.taxi.module.model.vo.LocationNewPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.LocationNewRespVO;
import com.holdtime.jtt809.taxi.module.model.vo.LocationNewSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.LocationNewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 车辆定位信息")
@RestController
@RequestMapping("/taxi/location-new")
@Validated
public class LocationNewController {

    @Resource
    private LocationNewService locationNewService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆定位信息")
    @PreAuthorize("@ss.hasPermission('taxi:location-new:create')")
    public CommonResult<String> createLocationNew(@Valid @RequestBody LocationNewSaveReqVO createReqVO) {
        return success(locationNewService.createLocationNew(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆定位信息")
    @PreAuthorize("@ss.hasPermission('taxi:location-new:update')")
    public CommonResult<Boolean> updateLocationNew(@Valid @RequestBody LocationNewSaveReqVO updateReqVO) {
        locationNewService.updateLocationNew(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆定位信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('taxi:location-new:delete')")
    public CommonResult<Boolean> deleteLocationNew(@RequestParam("id") String id) {
        locationNewService.deleteLocationNew(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆定位信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('taxi:location-new:query')")
    public CommonResult<LocationNewRespVO> getLocationNew(@RequestParam("id") String id) {
        LocationNewDO locationNew = locationNewService.getLocationNew(id);
        return success(BeanUtils.toBean(locationNew, LocationNewRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆定位信息分页")
    @PreAuthorize("@ss.hasPermission('taxi:location-new:query')")
    public CommonResult<PageResult<LocationNewRespVO>> getLocationNewPage(@Valid LocationNewPageReqVO pageReqVO) {
        PageResult<LocationNewDO> pageResult = locationNewService.getLocationNewPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LocationNewRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆定位信息 Excel")
    @PreAuthorize("@ss.hasPermission('taxi:location-new:export')")
    public void exportLocationNewExcel(@Valid LocationNewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LocationNewDO> list = locationNewService.getLocationNewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆定位信息.xls", "数据", LocationNewRespVO.class,
                        BeanUtils.toBean(list, LocationNewRespVO.class));
    }

}